import axios from 'axios'
const instance = axios.create({
  // baseURL: 'http:127.0.0.1:3000/api'
})
// 请求拦截器
instance.interceptors.request.use(config => {
  let userInfo = localStorage.getItem('userInfo')
  try { userInfo = JSON.parse(userInfo) || {} } catch (err) { userInfo = {} };
  if (userInfo?.authorization) config.headers.authorization = userInfo?.authorization
  return config
}, err => {
  return Promise.reject(err)
})
// 响应拦截器
instance.interceptors.response.use(res => res, err => {
    let { message } = err;
    if (message.includes("Network Error")) {
      message = "后端接口连接异常";
    }else if (message.includes("timeout")) {
      message = "系统接口请求超时";
    }else if (message.includes("Request failed with status code")) {
      message = "系统接口" + message.substr(message.length - 3) + "异常";
    }
    return { data: { code: -1, msg: message, data: [] }}
  }
)
export default instance
